package com.leslie.wukong.biz.product.handler;

import com.leslie.wukong.biz.product.domain.vo.ProductVO;
import com.leslie.wukong.common.core.constants.HttpStatus;
import com.leslie.wukong.common.core.constants.ValidationConstants;
import com.leslie.wukong.common.core.domain.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * ClassName: NullValueCheckHandler
 * Package: com.leslie.wukong.biz.product.handler
 * Description:
 * @date: 2024/12/3 18:12
 * @author: itcoder
 */
@Component
@Slf4j
public class NullValueCheckHandler extends AbstractCheckHandler {
    @Override
    public RestResult handle(ProductVO param) {
        log.info("空值校验 Handler 开始....");
        // 降级: 如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown()) {
            log.info("空值校验 Handler 已降级, 跳过空值校验 Handler....");
            return super.next(param);
        }
        // 参数必填校验
        if (Objects.isNull(param)) {
            return RestResult.error(HttpStatus.VALIDATION, ValidationConstants.PARAM_NULL_ERROR);
        }
        if (Objects.isNull(param.getSkuId())) {
            return RestResult.error(HttpStatus.VALIDATION, ValidationConstants.PARAM_SKU_NULL_ERROR);
        }
        if (Objects.isNull(param.getPrice())) {
            return RestResult.error(HttpStatus.VALIDATION, ValidationConstants.PARAM_PRICE_NULL_ERROR);
        }
        if (Objects.isNull(param.getStock())) {
            return RestResult.error(HttpStatus.VALIDATION, ValidationConstants.PARAM_STOCK_NULL_ERROR);
        }
        log.info("空值校验 Handler 通过....");

        return super.next(param);
    }
}
